import 'package:flutter/material.dart';
import 'package:hzy_normal_tool/hzy_normal_tool.dart';

import '../../common/index.dart';
import '../../tools/index.dart';

import 'eg_getx_list_v.dart';
import 'eg_state_list_v.dart';

class EgPage extends StatefulWidget {
  const EgPage({super.key});

  @override
  State<EgPage> createState() => _EgPageState();
}

class _EgPageState extends State<EgPage> with SingleTickerProviderStateMixin {
  List<Widget> pageS = [
    EgGetxListV(),
    const EgStateListV(),
  ];

  late TabController tabController;

  int index = 0;
  @override
  void initState() {
    super.initState();
    tabController = TabController(
      length: pageS.length,
      vsync: this,
    );
    tabController.addListener(() {
      index = tabController.index;
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    Widget body = TabBarView(
      controller: tabController,
      children: pageS.map((e) => CommonKeepAliverWrapper(child: e)).toList(),
    );
    body = SizedBox(
      width: ScreeTool.screenWidth,
      height: ScreeTool.screenHeight,
      child: body,
    );
    body = Stack(
      children: [
        body,
        createBottomTipWidget(),
        createCloseBtn(context),
      ],
    );
    return body;
  }

  createBottomTipWidget() {
    Widget body = HzyBtnWidget(
      width: 60,
      height: 60,
      decoration: BoxDecoration(
        color: Colors.black.withOpacity(0.4),
        borderRadius: BorderRadius.circular(16),
      ),
      imageWidget: const Icon(
        Icons.change_circle_rounded,
        color: Colors.white,
      ),
      space: 0,
      fontSize: 12,
      fontColor: Colors.white,
      btnLayoutType: BtnLayoutType.topImg,
      text: index == 1 ? 'GetX' : 'State',
      tapCall: () {
        tabController.animateTo(index == 1 ? 0 : 1);
      },
    );

    body = Positioned(
      right: 20,
      bottom: configSafeBottomHeight() + 10,
      child: body,
    );
    return body;
  }

  createCloseBtn(BuildContext context) {
    Widget body = IconButton(
      onPressed: () {
        currentGoback();
      },
      icon: const Icon(
        Icons.close,
        color: Colors.white,
      ),
    );
    body = Positioned(
      left: 20,
      bottom: configSafeBottomHeight() + 10,
      child: body,
    );

    body =
        !HzyAppBarGenerator.isFirstPage(context: context) ? Container() : body;
    return body;
  }
}
